/**
 * @Title: Offer.java 
 * @Description: TODO
 * @author <a href="mailto:psammead@126.com">Psammead</a>
 * @date 2011-7-16 上午10:40:32 
 * @version 1.0
 * Copyright 2011 by Psammead
 */
package com.psammead.dms.offer.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.psammead.dms.common.core.bo.AbstractBaseBO;
import com.psammead.dms.project.entity.Project;
import com.psammead.dms.system.sercurity.entity.SysUser;

/**
 * @ClassName: Offer
 * @Description: TODO
 * @author <a href="mailto:psammead@126.com">psammead</a>
 * @version 1.0
 * @date 2011-7-16
 */
@Entity(name="Offer")
@Table(name="tb_of_offer")
public class Offer extends AbstractBaseBO implements Serializable{

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = -4741180291909093665L;
	private long id;
	private OfferTemplate offerTemplate;
	private Project project;
	private String status;
	private SysUser creator;
	private SysUser updater;
	private Date createTime;
	private Date submitTime;
	private Date updateTime;
	
	private String address;
	private Double materialCost;
	private Double agencyFee;
	private Double total;
	
	private Set<MaterialType> types=new HashSet<MaterialType>();
	private Set<Material> materials=new HashSet<Material>();
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="offer_id")
	@Override
	public long getId() {
		return id;
	}
	
	public void setId(long id) {
		this.id = id;
	}

	@ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.MERGE,
			CascadeType.REFRESH},optional=true)
	@JoinColumn(name="template_id")
	public OfferTemplate getOfferTemplate() {
		return offerTemplate;
	}

	public void setOfferTemplate(OfferTemplate offerTemplate) {
		this.offerTemplate = offerTemplate;
	}

	@ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.MERGE,
			CascadeType.REFRESH},optional=true)
	@JoinColumn(name="project_id")
	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	@Column(name="status")
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	@ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.MERGE,
			CascadeType.REFRESH},optional=true)
	@JoinColumn(name="creator")
	public SysUser getCreator() {
		return creator;
	}

	public void setCreator(SysUser creator) {
		this.creator = creator;
	}

	@ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.MERGE,
			CascadeType.REFRESH},optional=true)
	@JoinColumn(name="updater")
	public SysUser getUpdater() {
		return updater;
	}

	public void setUpdater(SysUser updater) {
		this.updater = updater;
	}

	@Column(name="create_time")
	@Temporal(value=TemporalType.DATE)
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name="submit_time")
	@Temporal(value=TemporalType.DATE)
	public Date getSubmitTime() {
		return submitTime;
	}

	public void setSubmitTime(Date submitTime) {
		this.submitTime = submitTime;
	}

	@Column(name="update_time")
	@Temporal(value=TemporalType.DATE)
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	@Column(name="address")
	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
	@Column(name="material_cost")
	public Double getMaterialCost() {
		return materialCost;
	}

	public void setMaterialCost(Double materialCost) {
		this.materialCost = materialCost;
	}

	@Column(name="agency_fee")
	public Double getAgencyFee() {
		return agencyFee;
	}

	public void setAgencyFee(Double agencyFee) {
		this.agencyFee = agencyFee;
	}

	@Column(name="total")
	public Double getTotal() {
		return total;
	}

	public void setTotal(Double total) {
		this.total = total;
	}

	@ManyToMany(cascade = CascadeType.REMOVE)
	@JoinTable(name = "tr_of_offere_type", 
			joinColumns = {@JoinColumn(name = "offer_id",nullable = false)}, 
			inverseJoinColumns = @JoinColumn(name = "type_id",nullable = false))
	public Set<MaterialType> getTypes() {
		return types;
	}

	public void setTypes(Set<MaterialType> types) {
		this.types = types;
	}

	public void addType(MaterialType type){
		if(!this.types.contains(type)){
			this.types.add(type);
		}
	}
	public void removeType(MaterialType type){
		this.types.remove(type);
	}

	@OneToMany(mappedBy="offer",cascade=CascadeType.ALL)
	public Set<Material> getMaterials() {
		return materials;
	}

	public void setMaterials(Set<Material> materials) {
		this.materials = materials;
	}
}
